﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace Albatross.WPF.Common.Views {
	/// <summary>
	/// convert result of and\or operation to visibility
	/// </summary>
	public class VisibilityMultiValueConverter : IMultiValueConverter {

		private Visibility _collapsedOrHidden;
		public Visibility CollapsedOrHidden {
			get { return _collapsedOrHidden; }
			set {
				if (value == Visibility.Visible) {
					throw new ArgumentException();
				}
				_collapsedOrHidden = value;
			}
		}
		public bool Reversed {
			get;
			set;
		}

		public string Operation {
			get;
			set;
		}

		public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (string.Equals(Operation, "and", StringComparison.InvariantCultureIgnoreCase)) {
				foreach (object value in values) {
					if (value == DependencyProperty.UnsetValue || !System.Convert.ToBoolean(value)) {
						return Reversed? Visibility.Visible: CollapsedOrHidden;
					}
				}
				return Visibility.Visible;
			} else if (string.Equals(Operation, "or", StringComparison.InvariantCultureIgnoreCase)) {
				foreach (object value in values) {
					if (value != DependencyProperty.UnsetValue && System.Convert.ToBoolean(value)) {
						return Reversed? CollapsedOrHidden: Visibility.Visible;
					}
				}
			} else {
				throw new NotSupportedException();
			}
			return CollapsedOrHidden;
		}

		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
			throw new NotSupportedException();
		}
	}

}
